package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.DecimalFormat;
import java.util.Date;
import java.text.SimpleDateFormat;

public class ex4 extends JFrame {

	private JPanel contentPane;
	private JLabel lblNewLabel;
	private JTextField textField;
	private JButton btn1;
	private JButton btn2;
	private JScrollPane scrollPane;
	private JTextArea textArea;
	private JButton btnNewButton_1;

	Chul[] chul = new Chul[10];
	int pos = 0; // 데이터가 저장될 위치.
	boolean flag = false; // 한번이라도 10건 이상.

	// Chul[] c = new Chul[10];
	// SimpleDateFormat sf = new SimpleDateFormat("yy-MM-dd hh:mm");
	// int count=0;

	/**
	 * Launch the application.
	 */

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ex4 frame = new ex4();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ex4() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getLblNewLabel());
		contentPane.add(getTextField());
		contentPane.add(getBtn1());
		contentPane.add(getBtn2());
		contentPane.add(getScrollPane());
		contentPane.add(getBtnNewButton_1());

	}

	public void check(boolean b) { // true

		if (textField.getText().equals("")) {
			JOptionPane.showMessageDialog(null, "아이디를 입력하세요.");
			return;
		}

		Chul c =null;
		String temp="";
		if (b) {
			c = new Chul(textField.getText(), new Date(), null, new Date());
			temp = "출근";
		} else {
			c = new Chul(textField.getText(), null, new Date(), new Date());
			temp = "퇴근";
		}
		chul[pos] = c;
		if (pos == 9) {
			pos = -1;
			flag = true;
		}
		pos++;
		
	

		textArea.insert(c.toString() + temp + "\n",0);
		textArea.setCaretPosition(0);
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("아이디");
			lblNewLabel.setBounds(121, 10, 57, 15);
		}
		return lblNewLabel;
	}

	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.setBounds(190, 7, 116, 21);
			textField.setColumns(10);
		}
		return textField;
	}

	private JButton getBtn1() {
		if (btn1 == null) {
			btn1 = new JButton("출근");
			btn1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					check(true);

					// if(count ==10){
					// for(int i=0; i<c.length-1; i++){
					// c[i]=c[i+1];
					// }
					// c[9]=new Chul();
					// c[9].id=textField.getText();
					// c[9].checkIn=new Date();
					// }
					// for (int i = 0; i < c.length; i++) {
					// if (c[i] == null) {
					// c[i] = new Chul();
					// c[i].id = textField.getText();
					// c[i].checkIn = new Date(); // Date형식으로 저장하고 빼내올때 simple을
					// 사용해서 출력
					// System.out.println("c[i] : "+ i);
					// count++;
					// break;
					// }
					// }
					//
					// textArea.append(textField.getText() + " ");
					// textArea.append(sf.format(new Date()) + " ");
					// textArea.append("입실\n");
				}
			});
			btn1.setBounds(98, 40, 97, 23);
		}
		return btn1;
	}

	private JButton getBtn2() {
		if (btn2 == null) {
			btn2 = new JButton("퇴근");
			btn2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					check(false);

					// String id = textField.getText();
					// int i=0;
					// if(count==10){
					// c[9].checkOut=new Date();
					// }
					// for (i = 0; i <= c.length; i++) {
					// System.out.println("c[i]"+i);
					// if (c[i].id.equals(id)) {
					// c[i].checkOut = new Date();
					// break;
					// }
					// }
					// textArea.append(textField.getText() + " ");
					// textArea.append(sf.format(new Date()) + " ");
					// textArea.append("퇴실\n");
				}
			});
			btn2.setBounds(226, 40, 97, 23);
		}
		return btn2;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(98, 80, 223, 124);
			scrollPane.setViewportView(getTextArea());
		}
		return scrollPane;
	}

	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
		}
		return textArea;
	}

	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("출퇴근 목록확인");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					ChulList clist = new ChulList(chul,pos,flag);
					clist.setBounds(300,200,300,500);
					clist.setVisible(true);
					
					
					// ex41 e = new ex41();
					// e.setD(c);
					// e.setVisible(true);

				}
			});
			btnNewButton_1.setBounds(98, 214, 225, 38);
		}
		return btnNewButton_1;
	}
}
